package com.warm.kafkalearn.controller;

import com.warm.kafkalearn.pojo.User;
import com.warm.kafkalearn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/show")
    public List<User> getUser(int age){
        return userService.getUser(age);
    }

    @RequestMapping("/index")
    public Map<String, String> Index(){
        Map map = new HashMap<String, String>();
        map.put("北京","北方城市");
        map.put("深圳","南方城市");
        return map;
    }
    @RequestMapping(value = "/getusernamebyid")
    public String getUserNameById(Integer uid)
    {
        return userService.getUserNameById(uid);
    }

    @RequestMapping(value = "/setusernamebyid")
    public String setUserNameById(Integer uid, String uname)
    {
        userService.setUserNameById(uid, uname);
        return "设置成功";
    }

    @RequestMapping(value = "/jedistest")
    public String jedisTest(){
        // 创建一个jedis对象
        Jedis jedis = new Jedis("ip", 6379);
        // 直接调用jedis对象的方法，方法名称和redis的命令一致
        jedis.set("key1", "test01");
        String key1 = jedis.get("key1");
        System.out.println(key1 + " " + key1);
        // 关闭jedis
        jedis.close();
        return key1;
    }
    @RequestMapping(value = "/cache/getuserbyid")
    public User getUserDetailByUid(int uid){
        try {
            return userService.getUserDetailByUid(uid);
        }catch (Exception e){
            System.out.println(e.toString());
            return null;
        }
    }
    @RequestMapping(value = "/cache/updateUserInfo")
    public int updateUserInfo(int uid,int age){
        User user = new User();
        user.setId(uid);
        user.setAge(age);
        User u = userService.updateUserInfo(user);
        return u == null ? 0 : user.getId();
    }
    @RequestMapping(value = "/cache/deleteUserInfoById")
    public int deleteUserInfoById(int uid){
        return userService.deleteUserInfoById(uid);
    }
}
